package com.kuaiditu.assistant.api;

import com.alibaba.fastjson.JSON;
import com.kuaiditu.assistant.api.assistant_api.base.AssistantResponse;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Created by Eenie on 2017/6/7.
 * Email: eeniework@gmail.com
 * Des:
 */

public class FileManager {

    private FileService mForumService;

    public FileManager(OkHttpClient client) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://app.kuaiditu.com")
                .client(client)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        mForumService = retrofit.create(FileService.class);
    }


    public Observable<AssistantResponse> uploadImg(String path) {
//        Map<String, Object> param = new HashMap<>();
        MultipartBody.Part body = null;

        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
//        {"source":"and","object":"user"    }
        Map<String, Object> param = new HashMap<>();
        param.put("source", "and");
        param.put("object", "parTimeApi");
        MultipartBody.Part paramPart = MultipartBody.Part.createFormData("param", JSON.toJSONString(param));


        return mForumService.uploadImage(paramPart, body);
    }


}
